﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using XQ.Framework.core;

namespace XQ.Framework.io
{
    /// <summary>
    /// Log 的摘要说明
    /// </summary>
    public class Log
    {
        public int MaxSize { get; set; }
        public string LogPath { get; set; }
        //单个日志最大体积 5MB
        public Log()
        {
            MaxSize = 1024 * 1024 * 5;
            LogPath = @"c:\hiwjcn\";
        }

        public bool AddLog(string content)
        {
            string log = string.Format("记录时间：{0}\n", DateTime.Now);
            log += string.Format("记录内容：{0}\n", content);
            log += "==================================================================\n";
            return WriteLog(LogPath, log);
        }

        public static readonly object LogLocker = new object();
        /// <summary>
        /// 写入log
        /// </summary>
        /// <param name="path">log存放绝对目录</param>
        /// <param name="content">需要写入的logo</param>
        private bool WriteLog(string path, string content)
        {
            lock (LogLocker)
            {
                StreamWriter sw = null;
                try
                {
                    //日志目录如果不存在就创建
                    DirectoryInfo di = new DirectoryInfo(path);
                    if (!di.Exists)
                    {
                        di.Create();
                    }
                    //通过算法选择适合的文件存放
                    FileInfo fi = PrepareFile(path);
                    if (fi == null || !fi.Exists) { return false; }
                    sw = new StreamWriter(fi.OpenWrite());
                    sw.BaseStream.Seek(0, SeekOrigin.End);
                    sw.WriteLine(content);
                    sw.Flush();

                    return true;
                }
                catch (Exception e)
                {
                    return false;
                }
                finally
                {
                    IOHelper.closeStreamWriter(sw);
                }
            }
        }
        /// <summary>
        /// 在目录中选择合适的文件
        /// </summary>
        /// <param name="path">目录</param>
        /// <returns>返回文件信息</returns>
        private FileInfo PrepareFile(string path)
        {
            DateTime date = DateTime.Now;
            //文件名，不加路径，不加后缀名
            string filename = date.Year.ToString() + "-" + date.Month.ToString() + "-" + date.Day.ToString();
            int i = 1;
            FileInfo info = null;
            while (true)
            {
                info = new FileInfo(path + filename + "_" + i + ".log");
                if (info.Exists)
                {
                    ++i;
                    if (info.IsReadOnly)
                    {
                        continue;
                    }
                    if (info.Length <= MaxSize)
                    {
                        break;
                    }
                }
                else
                {
                    info.Create().Close();
                    break;
                }
            }
            info.Refresh();
            return info;
        }
    }
}